<?php

namespace app\admin\model;
use think\Model;
use app\admin\validate\Category as va_Category;
use think\exception\ValidateException;
class Category extends Model
{


    // 模型初始化
    protected static function init()
    {
        //TODO:初始化内容
    }
    protected $field = ['name','sort','status','parent_id','icon'];
    public function getStatusAttr($value)
    {
        $status = [1=>'正常',2=>'隐藏'];
        return $status[$value];
    }


    public function getGoods(): \think\model\relation\HasMany
    {

        return $this->hasMany(\app\wap\model\Goods::class,'cate_id','id');

    }

    public static function categoryList(){


        $where['status']=1;
        $where['store_id']=session('store_id');
        $list=self::where($where)->field('id,name,parent_id,sort,status')->order('sort asc')->select();


        return getTree2($list->toArray());
    }


    public static function insertCategory(){
        try {
            $add['name']=input('name','','trim');
            $add['parent_id']=input('parent_id','','trim');
            $add['sort']=input('sort','','trim');
            $add['status']=input('status','','trim');
            $add['icon']=input('icon','','trim');
            $add['store_id']=session('store_id');
            $result = validate(va_Category::class)->check($add);
            if(!empty($result)){

                $result=self::create($add);
                if($result){
                    $data = array(
                        'code' => 200,
                        'msg' => '成功!',
                        'url'=>url('/nav/lists')->build()
                    );

                    return $data;
                }else{
                    $data = array(
                        'code' => 40002,
                        'msg' => '失败!',
                    );
                    return $data;
                }
                // dump($result);die;
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }

    }

    public static function modifyCategory(){
        try {


            $result = validate(va_Category::class)->check(input(''));

            if (!empty($result)) {

                $add['name']=input('name','','trim');
                $add['parent_id']=input('parent_id','','trim');
                $add['sort']=input('sort','','trim');
                $add['status']=input('status','','trim');
                $add['icon']=input('icon','','trim');
                $result=self::where(['id'=>input('id')])->update($add);

                if(!empty($result)){
                    $data = array(
                        'code' => 200,
                        'msg' => '成功!',
                        'url'=>url('/nav/lists')->build()
                    );

                    return $data;
                }else{
                    $data = array(
                        'code' => 40002,
                        'msg' => '失败!',
                    );
                    return $data;
                }
                // dump($result);die;
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息

            return $e->getError();
        }

    }


    public static function del(){

        $id=input('id');


        $res=self::where(['parent_id'=>$id])->find();
        if(!empty($res)){
            $msg=[
                'code'=>4026,
                'msg'=>'该分类下面有子类不能删除'
            ];
            return $msg;
        }
        $res=self::where(['id'=>$id])->delete();
        if(!empty($res)){
            $msg=[
                'code'=>200,
                'msg'=>'success'
            ];
        }else{
            $msg=[
                'code'=>40040,
                'msg'=>'删除失败'
            ];
        }

        return $msg;

    }





}